﻿$(function () {
    //2.初始化页面
    var oPageInit = new PageInit();
    oPageInit.Init();
});

var PageInit = function () {
    var oInit = new Object();
    var postdata = {};

    var table, laydate, form, layer;
    var active;
    var tableId = 'tb_list';
    //初始化页面上面的按钮事件
    oInit.Init = function () {
        layui.use('table', function () {
            table = layui.table
          , laydate = layui.laydate
          , form = layui.form
          , layer = layui.layer;
            oInit.loadData();
        })
    }

    //初始化Table,加载数据
    oInit.loadData = function () {
        //方法级渲染
        table.render({
            elem: '#' + tableId
            , id: tableId
        , loading: true
        , toolbar: '#toolbar'
        , defaultToolbar: ['']  //工具栏右侧的图标按钮
        , url: Root + 'GTTaskTracking/GetTaskTracking'
        , cols: [[
        { checkbox: true, fixed: true }, { type: 'numbers', title: '序号' }
        , { field: 'id', title: 'ID', sort: true, fixed: true, hide: true }
        , { field: 'project_name', title: '任务名称', sort: true }
        , { field: 'type', title: '单独/合作', width: 100 }
        , {
            field: 'partner', title: '参与人', width: 150,
            
        }
        , { field: 'remark', title: '任务说明', width: 200 }
        , {
            field: 'begin_date', title: '开始日期', width: 110,
            templet: function (row) {
                return row.begin_date == null ? '' : changeDateFormat(row.begin_date);
            }
        }
        , {
            field: 'end_date', title: '完成日期', width: 110,
            templet: function (row) {
                return row.end_date == null ? '' : changeDateFormat(row.end_date);
            }
        }
        , {
            field: 'is_finished', title: '完成情况', width: 100,
            templet: function (row) {
                return row.is_finished == 1 ? "已完成" : "未完成";
            }
        }
        , { fixed: 'right', title: '操作', toolbar: '#tableBar', width: 120 }
        ]]
        , page: true
        , limit: 10
            //, cellMinWidth: 80
        , height: 'full-300'
        });

        var $ = layui.$;
        active = {
            reload: function () {
                var txt_search = $('#txt_search');
                var temp = {
                    SearchText: txt_search.val(),
                };

                //执行重载
                table.reload(tableId, {
                    method: 'post',
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                  , where: temp
                }, 'data');
            }
        };

        $('#formSearch .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条增删改按钮
        zpActive.tableTool(table, layer, form, null, null, null, null, oInit.NewEventsCode, oInit.Delete, oInit.LoadTypeInit);

        //监听提交按钮
        form.on('submit(save)', function (data) {
            var rs = oInit.Save(data.field);    //保存数据
            if (rs) {
                active.reload();    //重新加载table数据
                layer.closeAll();   //关闭弹出层
            }
            return false;   //true刷新当前页面
        });

        form.on('select(selType)', function (data) {
            var val = data.value;
            var text = data.elem[data.elem.selectedIndex].text;
            if (val == "合作") { //文字类型
                $('#partner_div').show();
            } else {
                $('#partner_div').hide();
                $('#txtPartner').val('');
                form.render();
            }
        });
    }

    oInit.NewEventsCode = function (formSelector, obj) {
        var data = obj.data;
        if (obj.event == 'edit') {
            debugger
            if (data.type == "合作") {
                $('#partner_div').show();
            } else {
                $('#partner_div').hide();
            }
        }
    }

    //保存-新增
    oInit.Save = function (formdata) {
        //var dto = decodeURIComponent(formdata, true);   //处理中文,后台需要解码
        var result = false;
        //静态添加节点
        $.ajax({
            type: "post",
            url: Root + 'GTTaskTracking/SaveTaskTracking',
            async: false,
            data: { dto: formdata },
            success: function (data) {
                if (data.SubmitResult) {
                    result = true;
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
        return result;
    }

    //删除
    oInit.Delete = function (arr_obj) {
        for (var i = 0; i < arr_obj.length; i++) {
            if (arr_obj[i].type == "超级管理员") {
                dialog.warning('超级管理员不能删除！');
                return;
            }
        }
        $.ajax({
            type: "post",
            url: Root + 'GTTaskTracking/DeleteTaskTracking',
            data: { dto: arr_obj },
            success: function (data) {
                if (data.SubmitResult) {
                    dialog.success('删除成功');
                    active.reload();
                } else {
                    dialog.danger('操作失败！Error Message:' + data.Message);
                }
            },
            error: function () {
                dialog.danger('操作失败！请重试或者联系管理员');
            },
            complete: function () {

            }
        });
    }

    return oInit;
}